String.prototype.endWith=function(s){
  if(s==null||s==""||this.length==0||s.length>this.length)
     return false;
  if(this.substring(this.length-s.length)==s)
     return true;
  else
     return false;
  return true;
 }

 String.prototype.startWith=function(s){
  if(s==null||s==""||this.length==0||s.length>this.length)
   return false;
  if(this.substr(0,s.length)==s)
     return true;
  else
     return false;
  return true;
 }
 
 //通用表单验证
 
 var rules = [
               {name: 'required', validate: function(value) {return ($.trim(value) == '');}, defaultMsg: '请输入内容'},
               {name: 'number', validate: function(value) {return (!/^\d+$/.test(value));}, defaultMsg: '请输入合法整数'},
               {name: 'decimal', validate: function(value) {return (!/^\d+(\.\d+)?$/.test(value));}, defaultMsg: '请输入合法数字（可带两位小数）'},
               {name: 'mail', validate: function(value) {return (!/^[a-zA-Z0-9]{1}([\._a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+){1,3}$/.test(value));}, defaultMsg: '请输入正确的邮箱地址。'},
               {name: 'char', validate: function(value) {return (!/^[a-z\_\-A-Z]*$/.test(value));}, defaultMsg: '请输入英文字符'},
               {name: 'chinese', validate: function(value) {return (!/^[\u4e00-\u9fff]$/.test(value));}, defaultMsg: '请输入汉字'},
   			{name:'qq',validate:function(value){return (!/^\d{5,10}$/.test(value));},defaultMsg:'请输入正确的qq号码'},//qq
   			{name:'phone',validate:function(value){//手机号
   								var flag_yd = /^1(3[4-9]|5[012789]|8[78])\d{8}$/.test(value);//移动
   								var flag_lt = /^1(3[0-3]|5[56]|8[56])\d{8}$/.test(value);//联通
   								var flag_dx = /^1([35]3|8[09])\d{8}$/.test(value);//电信
   								return !(flag_yd || flag_lt || flag_dx);
   							},defaultMsg:'请输入正确的手机号码'},
   			{name:'identitycard',validate:function(value){return (!/^\d{15}$|^\d{18}$/.test(value));},defaultMsg:'请输入正确的身份证号'},//身份证
   			{name:'tel',validate:function(value){return (!/\d{3}-\d{8}|\d{4}-\d{7}/.test(value));},defaultMsg:'请输入正确的电话号码'},//国内固定电话
   			{name:'zipcode',validate:function(value){return (!/^[1-9]\d{5}$/.test(value));},defaultMsg:'请输入正确的邮政编码'}//国内邮政编码
           ];
 var fieldState = false;
 
 function validateField(field, valid) { // 验证字段
     var el = $(field), error = false, errorMsg = '';
     for (i = 0; i < valid.length; i++) {
         var x = true, flag = valid[i], msg = (el.attr(flag + '-message')==undefined)?null:el.attr(flag + '-message');;
         if (flag.substr(0, 1) == '!') {
             x = false;
             flag = flag.substr(1, flag.length - 1);
         }

         for (j = 0; j < rules.length; j++) {
             var rule = rules[j];
             if (flag == rule.name) {
                 if (rule.validate.call(field, el.val()) == x) {
                     error = true;
                     errorMsg = (msg == null)?rule.defaultMsg:msg;
                     break;
                 }
             }
         }

         if (error) {break;}
     }

     var controls = el.parents('.controls'), controlGroup = el.parents('.control-group'), errorEl = controls.children('.help-block, .help-inline');

     if (error) {
         if (!controlGroup.hasClass('error')) {
             if (errorEl.length > 0) {
                 var help = errorEl.text();
                 controls.data('help-message', help);
                 errorEl.text(errorMsg);
             } else {
                 controls.append('<span class="help-inline">'+errorMsg+'</span>');
             }
             controlGroup.addClass('error');
         }
     } else {
         if (fieldState) {
             if (errorEl.length > 0) {
                 var help = controls.data('help-message');
                 if (help == undefined) {
                     errorEl.remove();
                 } else {
                     errorEl.text(help);
                 }
             }
             controlGroup.attr('class','control-group');
         } else {
             if (errorEl.length > 0) {
                 var help = errorEl.text();
                 controls.data('help-message', help);
             }
         }
     }
     return !error;
 };